Imports System
Imports System.String
Imports Microsoft.VisualBasic
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.Sqlclient

Partial Class ManualEntry
  Inherits System.Web.UI.Page

  Private ChangedText(144, 2) As String
  Private ChangedTextNdx As Integer

  'Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
  '  Dim cell As TableCell
  '  Dim control As Control
  '  If e.Row.RowType = DataControlRowType.DataRow Then
  '    For Each cell In e.Row.Cells
  '      For Each control In cell.Controls
  '        If TypeOf control Is TextBox Then
  '          CType(control, TextBox).Attributes.Add("ontextchange", "textchange") ' Page.ClientScript. '.GetPostBackEventReference(sender, "changedEdit$" & e.Row.RowIndex.ToString))
  '          CType(control, TextBox).Attributes.Add("autopostback", "true") ' Page.ClientScript. '.GetPostBackEventReference(sender, "changedEdit$" & e.Row.RowIndex.ToString))
  '          '            Page.ClientScript.RegisterForEventValidation(CType(control, TextBox).ID)
  '        End If
  '      Next
  '    Next
  '  End If

  'End Sub

  'Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  '  Dim mySqlConnection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("VendorStatsConnectionString").ToString)
  '  Dim mySqlCommand As SqlCommand = New SqlCommand("ManualEntry", mySqlConnection)
  '  mySqlCommand.CommandType = CommandType.StoredProcedure
  '  ' set this based on datatype

  '  '' setup parameters to pass to stored procedure
  '  mySqlCommand.Parameters.Add("@DataSourceVendorId", SqlDbType.Int).Value = ddlVendors.SelectedValue
  '  mySqlCommand.Parameters.Add("@StatsType", SqlDbType.Int).Value = ddlStatsType.SelectedValue
  '  Dim parmCollegeId As New SqlParameter("@CollegeId", SqlDbType.Int)
  '  Dim parmMonth As New SqlParameter("@MonthIn", SqlDbType.Char, 10)
  '  Dim parmStats As New SqlParameter("@Stats", SqlDbType.Int)

  '  mySqlCommand.Parameters.Add(parmCollegeId).Direction = ParameterDirection.Input
  '  mySqlCommand.Parameters.Add(parmMonth).Direction = ParameterDirection.Input
  '  mySqlCommand.Parameters.Add(parmStats).Direction = ParameterDirection.Input

  '  Dim s As String = ""

  '  Dim row As GridViewRow
  '  Dim i As Integer, j As Integer
  '  Dim stats As String
  '  Dim TextBoxId As String

  '  mySqlConnection.Open()

  '  ' loop through the months
  '  For i = 1 To 12
  '    TextBoxId = "TextBox" & i.ToString
  '    parmMonth.Value = ddlYear.SelectedValue & "-" & i & "-1"

  '    For j = 0 To GridView1.Rows.Count - 1
  '      row = GridView1.Rows(j)

  '      If row.RowType = DataControlRowType.DataRow Then
  '        parmCollegeId.Value = CType(row.FindControl("CollegeId"), Label).Text
  '        stats = CType(row.FindControl(TextBoxId), TextBox).Text
  '        If stats = "" Then
  '          parmStats.Value = 0
  '        Else
  '          parmStats.Value = stats
  '        End If

  '        mySqlCommand.ExecuteNonQuery()
  '      End If
  '    Next
  '  Next

  '  mySqlConnection.Close()
  'End Sub

  'Protected Sub sqlStats_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles sqlStats.DataBinding
  '  Const sql1 As String = "SELECT * FROM (SELECT Colleges.ShortName, Colleges.CollegeId, Month(Month) AS monthnum, "
  '  Const sql2 As String = " AS Quantity FROM CollegeDataSourceVendor INNER JOIN Stats ON CollegeDataSourceVendor.CollegeDataSourceVendorId = Stats.CollegeDataSourceVendorId INNER JOIN Colleges ON CollegeDataSourceVendor.CollegeId = Colleges.CollegeId WHERE (Stats.Month BETWEEN CONVERT(DATETIME, @BegDate, 102) AND CONVERT(DATETIME, @EndDate, 102)) AND (CollegeDataSourceVendor.DataSourceVendorId = @DataSourceVendorId) UNION SELECT ShortName, CollegeId, 0, 0 FROM Colleges) AS o PIVOT (sum(Quantity) FOR MonthNum IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) AS p ORDER BY ShortName"
  '  Select Case ddlStatsType.SelectedValue
  '    Case 0
  '      sqlStats.SelectCommand = sql1 & " Stats.Sessions " & sql2
  '    Case 1
  '      sqlStats.SelectCommand = sql1 & " Stats.Searches " & sql2
  '    Case 2
  '      sqlStats.SelectCommand = sql1 & " Stats.eBooks " & sql2
  '  End Select
  'End Sub

  Protected Sub ddlStatsType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlStatsType.SelectedIndexChanged
    If Page.IsPostBack Then
      sqlStats.DataBind()
    End If
  End Sub

  Protected Sub ddlVendors_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlVendors.SelectedIndexChanged
    If Page.IsPostBack Then
      sqlStats.DataBind()
    End If
  End Sub

  Protected Sub ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlYear.SelectedIndexChanged
    sqlStats.SelectParameters("BegDate").DefaultValue = ddlYear.SelectedValue + "-1-1"
    sqlStats.SelectParameters("EndDate").DefaultValue = ddlYear.SelectedValue + "-12-1"
    sqlStats.DataBind()
  End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim i As Integer

    If Not Page.IsPostBack Then
      ' load the year drop down
      For i = Today.Year To Today.Year - 5 Step -1
        ddlYear.Items.Add(i)
      Next

      ddlStatsType.DataBind()
      ddlStatsType.SelectedValue = 2
      ddlVendors.DataBind()
      ddlVendors.SelectedIndex = 0

      txtStatsTypeSave.Text = ddlStatsType.SelectedValue
      txtDSVIdSave.Text = ddlVendors.SelectedValue
      txtYearSave.Text = ddlYear.SelectedValue
    End If
  End Sub

  'anytime text is changed, add it to the changedtext index, along with month and college id so 
  ' we only touch the database for changed items. function called because onchange is set on text boxes
  Protected Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim thisTextBox As TextBox = DirectCast(sender, TextBox)
    Dim thisGridViewRow As GridViewRow = DirectCast(thisTextBox.Parent.Parent, GridViewRow)
    Dim row As Integer = thisGridViewRow.RowIndex
    ChangedText(ChangedTextNdx, 0) = thisTextBox.Text
    ChangedText(ChangedTextNdx, 1) = DirectCast(thisGridViewRow.FindControl("CollegeId"), Label).Text
    ChangedText(ChangedTextNdx, 2) = Mid(thisTextBox.ID, 8)
    ChangedTextNdx += 1
  End Sub

  Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    If Page.IsPostBack And ChangedTextNdx > 0 Then

      Dim i As Integer
      Dim year As String

      If txtYearSave.Text = "" Then
        txtStatsTypeSave.Text = ddlStatsType.SelectedValue
        txtDSVIdSave.Text = ddlVendors.SelectedValue
        txtYearSave.Text = ddlYear.SelectedValue
      End If
      year = txtYearSave.Text '   ddlYear.SelectedValue

      Dim mySqlConnection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("VendorStatsConnectionString").ToString)
      Dim mySqlCommand As SqlCommand = New SqlCommand("ManualEntry", mySqlConnection)
      mySqlCommand.CommandType = CommandType.StoredProcedure
      ' set this based on datatype

      '' setup parameters to pass to stored procedure
      mySqlCommand.Parameters.Add("@DataSourceVendorId", SqlDbType.Int).Value = txtDSVIdSave.Text
      mySqlCommand.Parameters.Add("@StatsType", SqlDbType.Int).Value = txtStatsTypeSave.Text
      Dim parmCollegeId As New SqlParameter("@CollegeId", SqlDbType.Int)
      Dim parmMonth As New SqlParameter("@MonthIn", SqlDbType.Char, 10)
      Dim parmStats As New SqlParameter("@Stats", SqlDbType.Int)

      mySqlCommand.Parameters.Add(parmCollegeId).Direction = ParameterDirection.Input
      mySqlCommand.Parameters.Add(parmMonth).Direction = ParameterDirection.Input
      mySqlCommand.Parameters.Add(parmStats).Direction = ParameterDirection.Input

      mySqlConnection.Open()

      ' loop through the months
      For i = 0 To ChangedTextNdx - 1
        If ChangedText(i, 0) = "" Then
          parmStats.Value = 0
        Else
          parmStats.Value = ChangedText(i, 0)
        End If
        parmCollegeId.Value = ChangedText(i, 1)
        parmMonth.Value = year & "-" & ChangedText(i, 2) & "-1"
        mySqlCommand.ExecuteNonQuery()
      Next

      mySqlConnection.Close()

    End If

    txtStatsTypeSave.Text = ddlStatsType.SelectedValue
    txtDSVIdSave.Text = ddlVendors.SelectedValue
    txtYearSave.Text = ddlYear.SelectedValue

  End Sub

End Class
